package com.test;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.c.Constant;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.site.bean.FmFactorDO;
import lj.netty.ToolNettyInit;
import lj.netty.bean.LJNettyInitContext;
import lj.netty.tool.LJTNetty;
import ntd.protocol.bo.Ntd212BO;
import ntd.util.frame.task.handle.fm.bo.FmDataRecordRule;
import ntd.util.modular.data.util.handle.constant.Constant212;
import ntd.util.modular.data.util.handle.tool.Tool212;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 212 数据模拟上传
 * @author: liuwenju
 * @date: 2023/6/6 17:05
 * @version: 1.0
 */
public class Mock212DataPush {

    public static void main(String[] args) {
        LJNettyInitContext ljNettyInitContext = new LJNettyInitContext();
        ljNettyInitContext.setHost("61.52.247.249");
        ljNettyInitContext.setPort(20113);

        ToolNettyInit toolNettyInit = new ToolNettyInit(ljNettyInitContext);

        ThreadUtil.execAsync(toolNettyInit::start);

        DateTime date = DateUtil.parseDate("2023-06-07 12:00:00");
        for (int i = 0; i < 100; i++) {
            String format = DateUtil.format(date, Constant.DATE_FORMAT_WLW_TWO);
            LJTNetty.pushAll(genRandom212Data("TEST-2023060601", format).to212Str().getBytes());
            LJTNetty.pushAll(genRandom212Data("TEST-2023060602", format).to212Str().getBytes());
            date = DateUtil.offsetHour(date, -2);
        }
//        StaticLog.info(genRandom212Data("TEST-2023060601", "20230607100000").to212Str());
    }

    private static Ntd212BO genRandom212Data(String mn, String dataTime) {
        Ntd212BO ntd212BO = Tool212.build212Bo(mn);
        ntd212BO.setST(Constant212.ST_31);
        ntd212BO.setCN(Constant212.CN_2061);
        Map<String, String> map = new HashMap<>();

        map.put("DataTime", dataTime);
        // 查询因子列表
        List<FmFactorDO> fmFactorDOS = FactoryFmOpt.FM_FACTOR_OPT.listByMn(mn);
        // 查询因子标准列表
        List<FmDataRecordRule> fmDataRecordRules = FactoryFmOpt.OPT_FM_FACTOR_RULE.listDataRuleBo(ListUtil.of(mn));

        Map<String, FmDataRecordRule> fmDataRecordRuleMap = CollStreamUtil.toIdentityMap(fmDataRecordRules, FmDataRecordRule::getFactorCode);

        for (FmFactorDO fmFactorDO : fmFactorDOS) {
//            map.put(fmFactorDO.getFactorCode() + "-Rtd", String.valueOf(RandomUtil.randomInt(1, 100)));
            FmDataRecordRule fmDataRecordRule = fmDataRecordRuleMap.get(fmFactorDO.getFactorCode());
            Double oMin = NumberUtil.max(fmDataRecordRule.getOMin(), 0d);
            Double oMax = NumberUtil.min(fmDataRecordRule.getOMax(), 100d);
            map.put(fmFactorDO.getFactorCode() + "-Avg", String.valueOf(NumberUtil.round(RandomUtil.randomDouble(oMin, oMax), 2)));
            map.put(fmFactorDO.getFactorCode() + "-Cou", String.valueOf(NumberUtil.round(RandomUtil.randomDouble(oMin, oMax), 2)));
        }
        ntd212BO.setCPData(map);
        return ntd212BO;
    }
}
